package gamedsl.event;

import java.util.ArrayList;
import java.util.HashMap;

import org.newdawn.penguin.Entity;

public class EventTracker {
	private static EventTracker instance = null;
	private HashMap<String, ArrayList<IEventHandler>> listeners;
	
	private int hello;
	
	
	public EventTracker(){
		listeners = new HashMap<String, ArrayList<IEventHandler>>();
		populateEvents();
	}
	
	public static EventTracker getInstance(){
		if(instance == null){
			instance = new EventTracker();
		}
		return instance;
	}
	
	private void someMethod(){
		
	}
	
	private void populateEvents(){
		listeners.put("MushroomRewardHitEvent", new ArrayList<IEventHandler>());
	}
	
	
	public void subscribe(String eventName, IEventHandler eventHandler){
		ArrayList<IEventHandler> handlers = listeners.get(eventName);
		if(handlers == null){
			//event does not exist
			System.out.println("Error: Event does not exists");
			return;
		}
		handlers.add(eventHandler);
		listeners.put(eventName, handlers);
	}
	
	public void triggerEvent(String eventName, Event event){
		ArrayList<IEventHandler> subscribers = listeners.get(eventName);
		for(int i = 0; i < subscribers.size();i++){
			subscribers.get(i).handleEvent(event);
		}
		
	}
	
	public void unsubscribeAll(String eventName, int entityId){
		ArrayList<IEventHandler> subscribers = listeners.get(eventName);
		for(int i = 0; i < subscribers.size(); i++){
			IEventHandler a = subscribers.get(i);
			if(a.getHandlerId() == entityId){
				subscribers.remove(i);
			}
		}
	}
}
